package com.kotiln.lgc.lib_network.thread;

/**
 * @date 2021/3/26
 */
public class ThreadPoolUtils {
    private static volatile ThreadPoolUtils mInstance;
    private final ThreadPool threadPool;

    private ThreadPoolUtils() {
        threadPool = new DefaultThreadPool.Builder().create();
    }

    public static ThreadPoolUtils getInstance() {
        if (mInstance == null) {
            synchronized (ThreadPoolUtils.class) {
                if (mInstance == null) {
                    mInstance = new ThreadPoolUtils();
                }
            }

        }
        return mInstance;
    }

    /**
     * 设置线程工厂类
     *
     * @param name 线程名称
     *
     * @return 返回
     */
    public ThreadPoolUtils setThreadFactory(String name) {
        threadPool.setThreadFactory(new MyThreadFactory(name));
        return this;
    }

    public void execute(Runnable r) {
        if (threadPool != null && null != r) {
            threadPool.startThread(r);
        }
    }
}
